package com.kakarote.hbase.config;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class HBaseConfig {

    @Value("${hbase.zookeeper.quorum}")
    private String zkQuorum;

    @Value("${hbase.zookeeper.port}")
    private String zkPort;

    @Value("${hbase.zookeeper.znode-parent}")
    private String zkZnodeParent;

    @Bean
    public org.apache.hadoop.conf.Configuration hbaseConfiguration() {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        config.set("hbase.zookeeper.quorum", zkQuorum);
        config.set("hbase.zookeeper.property.clientPort", zkPort);
        config.set("zookeeper.znode.parent", zkZnodeParent);
        return config;
    }

    @Bean(destroyMethod = "close")
    public Connection hbaseConnection() throws IOException {
        return ConnectionFactory.createConnection(hbaseConfiguration());
    }
}
